//
//  MGPassCodeControl.m
//  Mango
//
//  Created by 胡超 on 2021/6/27.
//  Copyright © 2021 HuChao. All rights reserved.
//

#import "MGPassCodeControl.h"
#import "UIView+Mango.h"

@interface MGPassCodeControl() <UITextFieldDelegate> {
    NSInteger _codeLength;
    UITextField *_hiddenTextField;
    UIButton *_contentView;
    NSMutableArray<UILabel *> *_codeLabels;
    __weak UILabel *_currentCodeLabel;
}
@end

@implementation MGPassCodeControl

- (instancetype)init {
    return [self initWithCodeLength:4];
}

- (instancetype)initWithCodeLength:(NSInteger)codeLength {
    if (self = [super init]) {
        _codeLength = codeLength;
        _codeSize = CGSizeMake(30, 30);
        _codeSpace = 10;
        _secureTextEntry = YES;
        _hiddenTextField = [[UITextField alloc] init];
        _hiddenTextField.keyboardType = UIKeyboardTypeNumberPad;
        _hiddenTextField.hidden = YES;
        _hiddenTextField.delegate = self;
        [self addSubview:_hiddenTextField];
        
        _contentView = [[UIButton alloc] initWithFrame:self.bounds];
        [_contentView addTarget:_hiddenTextField action:@selector(becomeFirstResponder) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_contentView];
        
        _codeLabels = [[NSMutableArray alloc] init];
        for (int i=0; i<codeLength; i++) {
            UILabel *codeLabel = [[UILabel alloc] init];
            codeLabel.size = self.codeSize;
            codeLabel.textColor = UIColor.blackColor;
            codeLabel.textAlignment = NSTextAlignmentCenter;
            codeLabel.font = [UIFont systemFontOfSize:18];
            codeLabel.backgroundColor = UIColor.lightGrayColor;
            [_codeLabels addObject:codeLabel];
            [_contentView addSubview:codeLabel];
        }
        [self layoutCodeboxs];
        [self setCurrentIndex:0];
    }
    return self;
}

- (void)layoutCodeboxs {
    for (NSInteger i=0; i<_codeLabels.count; i++) {
        UILabel *codeLabel = _codeLabels[i];
        codeLabel.size = self.codeSize;
        codeLabel.left = (codeLabel.width+self.codeSpace)*i;
        
        _contentView.size = CGSizeMake(codeLabel.right, codeLabel.height);
        self.size = _contentView.size;
        
        if (self.normalConfiguration) {
            self.normalConfiguration(codeLabel);
        }
    }
}

- (void)setCodeSize:(CGSize)codeSize {
    _codeSize = codeSize;
    [self layoutCodeboxs];
}

- (void)setCodeSpace:(CGFloat)codeSpace {
    _codeSpace = codeSpace;
    [self layoutCodeboxs];
}

- (void)setNormalConfiguration:(void (^)(UILabel * _Nonnull))normalConfiguration {
    _normalConfiguration = [normalConfiguration copy];
    [self layoutCodeboxs];
}

- (void)setCurrentIndex:(NSInteger)currentIndex {
    if (currentIndex < 0 || currentIndex >= _codeLength) {
        return;
    }
    if (self.normalConfiguration) {
        self.normalConfiguration(_currentCodeLabel);
    }
    _currentCodeLabel = _codeLabels[currentIndex];
    if (self.selectConfiguration) {
        self.selectConfiguration(_currentCodeLabel);
    }
}

- (BOOL)becomeFirstResponder {
    return [_hiddenTextField becomeFirstResponder];
}

- (void)didInput {
    if ([self.delegate respondsToSelector:@selector(passCodeControl:didInputCode:)]) {
        [self.delegate passCodeControl:self didInputCode:_hiddenTextField.text];
    }
}

- (NSString *)codeString {
    return _hiddenTextField.text;
}

#pragma mark - UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([string isEqualToString:@""]) {
        NSInteger index = textField.text.length-1;
        UILabel *codeLabel = _codeLabels[index];
        codeLabel.text = @"";
        self.currentIndex = index-1;
        [self performSelector:@selector(didInput) withObject:nil afterDelay:0.1];
        return YES;
    } else {
        if (textField.text.length >= _codeLength) {
            return NO;
        }
        NSInteger index = textField.text.length;
        UILabel *codeLabel = _codeLabels[index];
        codeLabel.text = self.secureTextEntry ? @"●" : string;
        self.currentIndex = index;
        [self performSelector:@selector(didInput) withObject:nil afterDelay:0.1];
        return YES;
    }
}

@end
